package com.csnj.common.utils;

import com.csnj.common.constant.CommonConstants;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ace on 2017/9/8.
 */
public class BaseContextHandler {
    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static String getUserID(){
        Object value = get(CommonConstants.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value, "0");
    }

    public static String getUsername(){
        Object value = get(CommonConstants.CONTEXT_KEY_USERNAME);
        return returnObjectValue(value, "系统");
    }

    public static Integer getUserIdInt() {
        return Integer.valueOf(getUserID());
    }

    public static String getToken(){
        Object value = get(CommonConstants.CONTEXT_KEY_USER_TOKEN);
        return StringHelper.getObjectValue(value);
    }

    public static void setToken(String token){set(CommonConstants.CONTEXT_KEY_USER_TOKEN,token);}

    public static void setUserID(String userID){
        set(CommonConstants.CONTEXT_KEY_USER_ID,userID);
    }

    public static void setUsername(String username){
        set(CommonConstants.CONTEXT_KEY_USERNAME,username);
    }

    private static String returnObjectValue(Object value, String defaultval) {
        return value==null ? defaultval :value.toString();
    }

    public static void remove(){
        threadLocal.remove();
    }

    public static String checkAndGetUserId() {
        Object value = get(CommonConstants.CONTEXT_KEY_USER_ID);
        if(value!=null) {
            return value.toString();
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request!=null) {
            HttpSession session = request.getSession();
            return (String)session.getAttribute(CommonConstants.CONTEXT_KEY_USER_ID);
        }
        return "";
    }

    public static String checkAndGetUserName() {
        Object value = get(CommonConstants.CONTEXT_KEY_USER_NAME);
        if(value!=null) {
            return value.toString();
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(request!=null) {
            HttpSession session = request.getSession();
            return (String)session.getAttribute(CommonConstants.CONTEXT_KEY_USER_NAME);
        }
        return "";
    }

}
